package entity;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import bean.JavaBean;
import jxl.Workbook;
import jxl.Sheet;
import dao.*;


public class ExcelToDb {
    public static boolean excelToDb(String path) {
        List< JavaBean > listExcel=ExcelToDb.getAllByExcel(path);
        boolean i=false;
        
        for (JavaBean jb : listExcel) {
            String id=jb.getId();
            if (!TeacherDao.checkId(id)) {
            	TeacherDao.insert(id, jb.getName(), jb.getAge(), jb.getTitle(), jb.getDepartment(), jb.getCourse(), jb.getPhone(),jb.getAddress());
            	i=true;
            }else {
            	TeacherDao.update(id, jb.getName(), jb.getAge(), jb.getTitle(), jb.getDepartment(), jb.getCourse(), jb.getPhone(),jb.getAddress());
            	i=true;
            }
        }
        return i;
    }
    
    public static List<JavaBean> getAllByExcel(String file){
        List<JavaBean> list=new ArrayList<JavaBean>();
        try {
            Workbook rwb=Workbook.getWorkbook(new File(file));
            Sheet rs=rwb.getSheet(0);
            int clos=rs.getColumns();
            int rows=rs.getRows();
           /* System.out.println("clos:"+clos+" rows:"+(rows-1));*/
            for (int i = 1; i < rows; i++) {
                for (int j = 0; j < clos; j++) {
                    String id=rs.getCell(j++, i).getContents();//默认最左边编号也算一列 所以这里得j++
                    String name=rs.getCell(j++, i).getContents();
                    String age=rs.getCell(j++, i).getContents();
                    String title=rs.getCell(j++, i).getContents();
                    String department=rs.getCell(j++, i).getContents();
                    String course=rs.getCell(j++, i).getContents();
                    String phone=rs.getCell(j++, i).getContents();
                    String address=rs.getCell(j++, i).getContents();
                    
                   /* System.out.println("id:"+id+"  name:"+name+"  age:"+age+"  title:"+title+"   department:"+ department+"   course:"+ course+"   phone:"+ phone+"   address:"+ address);*/
                    list.add(new JavaBean(id, name, age, title,department,course,phone,address));
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        return list;
        
    }
   
}
